package net.youdi.gateway.handler;

import lombok.extern.slf4j.Slf4j;
import net.youdi.gateway.context.GatewayLog;
import org.slf4j.MDC;
import org.slf4j.Marker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.CollectionUtils;

/**
 * @author:scl
 * @date:2019-05-16 12:46
 * @description
 */
@Slf4j(topic = "log.gateWay")
public class DefaultGateWayRequestCacheHandler implements GateWayRequestCacheHandler {
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;
    @Async
    @Override
    public void cache(GatewayLog gatewayLog) {
        taskExecutor.execute(()->writeLogInfo(gatewayLog));
    }
    private void writeLogInfo(GatewayLog gatewayLog) {

        StringBuffer logInfo = new StringBuffer("\n>>>>>>>>>>>>>>>>>>(Request)Start Timestamp:{}>>>>>>>>>>>>>>>>>>");
        logInfo.append("\n访问用户信息：{}");
        logInfo.append("\n(Request) Url :{}");
        logInfo.append("\n(Request) Headers:{}");
        logInfo.append("\n{},{},{}");
        logInfo.append("\n(Request)Method:{},IP:{},Host:{}");
        if(!CollectionUtils.isEmpty(gatewayLog.getQueryParams())){
            logInfo.append("\n(Request) Query Params:").append(gatewayLog.getQueryParams());
        }
        logInfo.append("\n(Request)ContentType:{},Content Length:{}");
        if(!CollectionUtils.isEmpty(gatewayLog.getFormData())){
            logInfo.append("\n(Request) FormData {}").append(gatewayLog.getFormData());
        }
        logInfo.append("\n(Request) body={}");
        logInfo.append("\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
        logInfo.append("\n(Response)HttpStatus:{}");
        logInfo.append("\n(Response)Headers:{}");
        logInfo.append("\n(Response)ContentType:{},Content Length:{}");
        logInfo.append("\n(Response)Original Path:{}");
        logInfo.append("\n(Response) body={}");
        logInfo.append("\n<<<<<<<<<<<<<<<<<<(Response)End Cost:{} ms<<<<<<<<<<<<<<<<<<");
        log.info(logInfo.toString(),gatewayLog.getStartTime(),gatewayLog.getUserInfo(),gatewayLog.getRequestUrl(),gatewayLog.getRequestHeaders()
                ,gatewayLog.getRequestIp(),gatewayLog.getRequestUrl().getHost(),gatewayLog.getRequestHeaders().get("user-agent")
                ,gatewayLog.getRequestMethod(), gatewayLog.getRequestIp(),gatewayLog.getRequestUrl().getHost()
                ,gatewayLog.getRequestContentType(),gatewayLog.getRequestContentLength(), gatewayLog.getRequestBody()
                ,gatewayLog.getResponseStatus()
                ,gatewayLog.getResponseHeaders(),gatewayLog.getResponseContentType(),gatewayLog.getResponseContentLength()
                , gatewayLog.getRequestUrl().getPath(), gatewayLog.getResponseBody(),(gatewayLog.getEndTime()-gatewayLog.getStartTime()));
        MDC.put("file","accessIp");
        log.info("{},{},{}",gatewayLog.getRequestIp(),gatewayLog.getRequestUrl().getHost(),gatewayLog.getRequestHeaders().get("user-agent"));
        MDC.clear();
    }
}
